package cn.edu.scau.cmi.yangqing.hibernate.client.control;


import java.net.URL;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.yangqing.factory.Creator;
import cn.edu.scau.cmi.yangqing.factory.CreatorPICC;
import cn.edu.scau.cmi.yangqing.factory.CreatorPingan;
import cn.edu.scau.cmi.yangqing.factory.Insurance;
import cn.edu.scau.cmi.yangqing.hibernate.domain.Student;
import cn.edu.scau.cmi.yangqing.hibernate.domain.StudentProperty;
import cn.edu.scau.cmi.yangqing.hibernate.domain.Tutor;
import cn.edu.scau.cmi.yangqing.hibernate.domain.TutorProperty;
import cn.edu.scau.cmi.yangqing.hibernate.service.StudentService;
import cn.edu.scau.cmi.yangqing.hibernate.service.TutorService;
import cn.edu.scau.cmi.yangqing.hibernate.util.StageManage;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;

public class studentControl implements Initializable {

	   @FXML
	   private TableView<TutorProperty> tutorTable;
	   
	   @FXML
	   private TableColumn<TutorProperty, Integer> idt;
	   
	   @FXML
	   private TableColumn<TutorProperty, String> namet;
	   
	   @FXML
	   private Label before;
	   
	   @FXML
	   private TextField tutorId;
	   @FXML 
	   private Button changeTutorButton;
	   
	   
	   @FXML
	   private Label after;
	   
	   private final ObservableList<TutorProperty> cellDataTutor = FXCollections.observableArrayList();
	   StudentService studentService = new StudentService();
	   TutorService tutorService = new TutorService();
	   
	   Integer id;
	   
	   public void initialize(URL location, ResourceBundle resources) {
		   List<Tutor> tutorList = tutorService.findAllTutor();
		   for (Tutor tutor : tutorList) {
			TutorProperty tutorProperty = new TutorProperty(new SimpleIntegerProperty(tutor.getId()),
					new SimpleStringProperty(tutor.getName()));
				cellDataTutor.add(tutorProperty);
		   }
		   idt.setCellValueFactory(cellDataTutor -> cellDataTutor.getValue().getId().asObject());
		   namet.setCellValueFactory(cellDataTutor -> cellDataTutor.getValue().getName());
		   tutorTable.setItems(cellDataTutor);
		   
		   id = Integer.valueOf(StageManage.id);
		   Student student = studentService.findStudentById(id);
		   String text = tutorService.findTutorById(student.getTutor().getId()).getName();
		   before.setText(text);
	   }

	   public void changeTheTutor(ActionEvent event) {
		   Integer idT = Integer.valueOf(tutorId.getText());
		   studentService.teachTutor(idT, id);
		   Student student = studentService.findStudentById(id);
		   String text = tutorService.findTutorById(student.getTutor().getId()).getName();
		   after.setText(text);
	   }
	   
}
